package com.erclab.internal.xpresso.security;

import java.util.HashMap;

import org.apache.log4j.Logger;

import com.erclab.internal.xpresso.conf.Settings;
import com.erclab.internal.xpresso.datavalidators.DataValidationError;
import com.erclab.internal.xpresso.exceptions.DataSourceException;
import com.erclab.internal.xpresso.exceptions.DirectoryException;
import com.erclab.internal.xpresso.exceptions.FormException;
import com.erclab.internal.xpresso.formelements.FormElement;
import com.erclab.internal.xpresso.forms.Form;
import com.erclab.internal.xpresso.forms.FormHelper;
import com.erclab.internal.xpresso.util.FormElementUtil;
import com.erclab.internal.xpresso.webapp.WebAppListener;

public class LoginFormHelper implements FormHelper {

	// Create the log for this class
	private static final Logger log = Logger.getLogger(WebAppListener.class);

	
	protected final String INVALID_CREDENTIALS = "Invalid credentials";
	
	public void beforeFormCreation(Form theForm) throws FormException {
		// Nothing to do

	}

	public void afterFormCreation(Form theForm) throws FormException {
		// Nothing to do

	}

	public void afterSubmission(Form theForm, HashMap keyValues)
			throws FormException, DataValidationError {
		
		log.info("Begin afterSubmission()");
		
		FormElement userElement = (FormElement)theForm.getFormElements().get(0);
		FormElement passwordElement = (FormElement)theForm.getFormElements().get(1);
		
		SecurityCredentials sc = new SecurityCredentials();
		String userName = ((String[])userElement.getUserValue())[0];
		sc.setUserName(userName);
		sc.setPassword(((String[])passwordElement.getUserValue())[0]);
		
		try {
			log.info("validating credentials for: " + userName);
			if (!Settings.getAccessController().validateCredentials(sc)){
				log.info("Invalid credentials for: " + userName);
				throw new DataValidationError(INVALID_CREDENTIALS);
			} else {
				log.info("Valid credentials for: " + userName);
				User user = theForm.getFormParameters().getUser();
				user.setUserName(userName);
				Settings.getUserDirectory().loadUserBean(user);
				setRememberFlag(theForm);
				
			}
		} catch (DataSourceException e) {
			throw new FormException("A Data source exception was generated: " + e);
		} catch (DirectoryException e) {
			throw new FormException("A Directory exception was generated: " + e);
		} 
				
	}

	protected void setRememberFlag(Form theForm) {
		String[] userValues = (String[])FormElementUtil.getElementValue(theForm, "RememberUser");
		if (userValues != null){
			String remember = userValues[0];
			if (remember != null){
				if (remember.equals("1")){
					String forwardUrl = theForm.getForwardUrl();
					if (forwardUrl.indexOf('?') > 0){
						theForm.setForwardUrl(forwardUrl + "&rememberUser=true");
					} else {
						theForm.setForwardUrl(forwardUrl + "?rememberUser=true");
					}
				}
			}
		}
	}

	public void afterModification(Form theForm, HashMap keyValues)
			throws FormException, DataValidationError {
		// Nothing to do
	}

}
